<template>
  <div style="height:100%;width:100%">
    <div class="searchBox" v-show="!search">
      <h4>查询学生个人成绩</h4>
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="80px" class="form">
        <el-form-item label="班级" prop="className">
          <el-input v-model="form.className"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="stuName">
          <el-input v-model="form.stuName"></el-input>
        </el-form-item>
        <el-form-item label="课程" prop="courseName">
          <el-input v-model="form.courseName"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width:100%;background:#DEE6EC" @click="getDetail">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="detailBox" v-show="search">
      <a href="javascript:;" class="back" @click="back">返回</a>
      <h4>
        {{form.stuName}}同学成绩单
        <a href="javascript:;" class="export">
          <span v-on:click="exportEx">成绩导出</span>
        </a>
      </h4>
      <div class="table">
        <el-table
          :data="tableData"
          style="width: 100%;height:100%:font-size:18px"
          row-class-name="body-font"
          header-row-class-name="header-bg"
          max-height="760"
        >
          <el-table-column prop="courseName" label="课程" width="300" align="center"></el-table-column>
          <el-table-column prop="examPaperName" label="试卷" width="180" align="center"></el-table-column>
          <el-table-column prop="stuName" label="姓名" width="180" align="center"></el-table-column>
          <el-table-column prop="examGrade" label="分数" align="center"></el-table-column>
          <!-- <el-table-column prop="address" label="班级排名" align="center"></el-table-column> -->
        </el-table>
      </div>
    </div>
  </div>
</template>
<script>
import { classList, exportStuExcel } from "@/api/student";
import { examStudentIndividualGrade } from "@/api/exam";
import axios from "axios";
export default {
  data() {
    return {
      form: { className: "", stuName: "", courseName: "" },
      rules: {
        className: [
          { required: true, message: "请输入班级名称", trigger: "blur" }
        ],
        stuName: [
          { required: true, message: "请输入学生名称", trigger: "blur" }
        ]
      },
      search: false,
      classList: [],
      loading: false,
      tableData: []
    };
  },
  methods: {
    getDetail() {
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          examStudentIndividualGrade(this.form).then(res => {
            if (res.code == 1) {
              this.tableData = res.response;
            }
          });
          this.search = true;
        } else {
          return false;
        }
      });
    },
    back() {
      this.$refs["ruleForm"].resetFields();
      this.search = false;
    },
    exportEx() {
      axios({
        method: "post",
        url: "/api/admin/excel/export_student_individual_grade", // 请求地址
        data: this.form, // 参数
        responseType: "blob", // 表明返回服务器返回的数据类型
        headers: {
          "Content-Type": "application/json"
        }
      }).then(res => {
        // 处理返回的文件流
        const blob = new Blob([res.data]);
        const fileName = this.form.stuName + "同学成绩单.xlsx";
        const elink = document.createElement("a");
        elink.download = fileName;
        elink.style.display = "none";
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href); // 释放URL 对象
        document.body.removeChild(elink);
      });
    },
    remoteClassMethod(query) {
      this.loading = true;
      classList({ className: query, pageIndex: 1, pageSize: 20 })
        .then(res => {
          this.loading = false;
          if (res.code == 1) {
            this.classList = res.response.list;
          }
        })
        .catch(res => {
          this.loading = false;
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.searchBox {
  width: 741px;
  height: 420px;
  margin: 62px auto;
  background-color: #f5f8fb;
  padding: 40px 170px;
  > h4 {
    line-height: 40px;
    font-size: 20px;
    text-align: center;
  }
  .form {
    margin-top: 20px;
  }
}
.detailBox {
  padding: 30px;
  height: 100%;
  width: 100%;
  position: relative;
  .back {
    position: absolute;
    line-height: 30px;
    left: 70px;
  }
  > h4 {
    font-size: 24px;
    line-height: 40px;
    text-align: center;
    .export {
      float: right;
      font-size: 16px;
      color: #007cdc;
      font-weight: 100;
    }
  }
}
</style>